package com.hangzhou.xyj.push;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.manager.NotifyManager;
import com.hangzhou.xyj.model.GroupPushMsgModel;
import com.hangzhou.xyj.model.type.EGroupPushType;
import com.hangzhou.xyj.model.type.EPushMessageType;
import com.hangzhou.xyj.ui.activity.MyMessageActivity;
import com.hangzhou.xyj.ui.activity.MyOfficalMessageActivity;
import com.hangzhou.xyj.ui.activity.WebActivity;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * Created by yaohuix on 2016/6/2  14:04
 * <p/>
 * 说明：清除通知的时候
 */
public class NotificationReceiver extends BroadcastReceiver {
    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.mContext = context;
        String path = intent.getStringExtra(DataKey.URL);
        String message = intent.getStringExtra(DataKey.MESSAGE);
        int pushType = intent.getIntExtra(DataKey.PUSH_TYPE, -1);
        if (isAppAlive()) {//app是打开的
            if (Integer.valueOf(CommonPreferences.getUserId(context)) != -1) {//登录的
                Intent i = new Intent(context, MyMessageActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            } else {
                Intent it;
                if (StringUtil.isEmpty(path)) {//无url的
                    it = new Intent(context, MyOfficalMessageActivity.class);
                    GroupPushMsgModel msgModel = new GroupPushMsgModel();
                    msgModel.setMsgTime(System.currentTimeMillis());//时间
                    msgModel.setGroupPic("drawable://" + R.drawable.ic_offical_head);//头像
                    msgModel.setMsgType(EGroupPushType.NOTIFICATION.getValue());//类型
                    msgModel.setContent(message);
                    msgModel.setToId("all");
                    it.putExtra(DataKey.OBJ, msgModel);
                    it.putExtra(DataKey.PUSH_TYPE, pushType);
                } else {//有url的
                    it = new Intent(context, WebActivity.class);
                    it.putExtra(DataKey.INDEX, path);
                    it.putExtra(DataKey.PUSH_TYPE, pushType);
                    it.putExtra(DataKey.TITLE, message);
                }
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(it);
                resetNotificationCount(EPushMessageType.valueOf(pushType));
            }
        } else {//app未启动
            Intent launchIntent = context.getPackageManager().
                    getLaunchIntentForPackage(context.getPackageName());
            launchIntent.putExtra(DataKey.IS_FROM_NOTIFICATION, true);
            launchIntent.putExtra(DataKey.URL, path);
            launchIntent.putExtra(DataKey.MESSAGE, message);
            launchIntent.putExtra(DataKey.PUSH_TYPE, pushType);
            launchIntent.setFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            context.startActivity(launchIntent);
        }
//        int pushType = intent.getIntExtra(DataKey.PUSH_TYPE, -1);
//        switch (EPushMessageType.valueOf(pushType)) {
//            case SINGLE_CHAT:
//                NotifyManager.mSingleChatCount = 0;//重置
//                break;
//            case GROUP_CHAT:
//                NotifyManager.mGroupChatCount = 0;
//                break;
//            case ADD_GROUP:
//                NotifyManager.mAddGroupCount = 0;
//                break;
//            default:
//            case SYSTEM:
//                NotifyManager.mSystemCount = 0;
//                break;
//        }
    }

    /***
     * app 是否打开
     *
     * @return
     */
    private boolean isAppAlive() {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
        for (ActivityManager.RunningTaskInfo info : list) {
            if (info.topActivity.getPackageName().equals(mContext.getPackageName())
                    && info.baseActivity.getPackageName().equals(mContext.getPackageName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 重置
     *
     * @param pushMessageType
     */
    private void resetNotificationCount(EPushMessageType pushMessageType) {
        switch (pushMessageType) {
            case SINGLE_CHAT:
                NotifyManager.mSingleChatCount = 0;//重置
                break;
            case GROUP_CHAT:
                NotifyManager.mGroupChatCount = 0;
                break;
            case ADD_GROUP:
                NotifyManager.mAddGroupCount = 0;
                break;
            default:
            case SYSTEM:
                NotifyManager.mSystemCount = 0;
                break;
        }
    }
}
